//201902104030_何琛选
package cn.edu.sdjzu.xg.bysj.service;

import cn.edu.sdjzu.xg.bysj.dao.CommonDao;
import cn.edu.sdjzu.xg.bysj.dao.UserDao;
import cn.edu.sdjzu.xg.bysj.domain.User;
import cn.edu.sdjzu.xg.bysj.domain.authority.Actor;
import com.alibaba.fastjson.JSONObject;
import util.JdbcHelper;
import util.bysjException;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;

public final class UserService {
	private UserDao userDao = UserDao.getInstance();
	private static UserService userService = new UserService();
	
	public UserService() {
	}
	
	public static UserService getInstance(){
		return UserService.userService;
	}

	public Collection<User> getUsers() throws SQLException{
		Connection conn = JdbcHelper.getConn();
		Collection<User> users = userDao.findAll(conn);
		JdbcHelper.close(conn);
		return users;
	}
	
	public User getUser(Integer id) throws SQLException {
		Connection connection = JdbcHelper.getConn();
		User user = userDao.find(id,connection);
		JdbcHelper.close(connection);
		return user;
	}

	public Collection<User> findAll() throws SQLException {
		Connection conn = JdbcHelper.getConn();
		Collection<User> users = userDao.findAll(conn);
		JdbcHelper.close(conn);
		return users;
	}
	
	public boolean updateUser(User user) throws SQLException {
		boolean result = false;
		Connection connection = JdbcHelper.getConn();
		result = userDao.update(user,connection);
		JdbcHelper.close(connection);
		return result;
	}

	public boolean addUser(User user) throws SQLException {
		boolean result = false;
		Connection connection = JdbcHelper.getConn();
		result = userDao.add(user,connection);
		JdbcHelper.close(connection);
		return result;
	}

	public boolean deleteUser(Integer id) throws SQLException, bysjException {
		boolean result = false;
		Connection connection = JdbcHelper.getConn();
		result = this.deleteUser(this.getUser(id));
		JdbcHelper.close(connection);
		return result;
	}
	
	public boolean deleteUser(User user) throws SQLException, bysjException {
		boolean result = false;
		Connection connection = JdbcHelper.getConn();
		//获得所有下一级单位
		int countTeacherOfDepartment = CommonDao.getInstance().
				countWhereForeignKey(
						connection,
						"user",
						"teacher_id",
						user.getId());
		//若没有二级单位，则能够删除
		if(countTeacherOfDepartment==0){
			result = userDao.delete(user,connection);
		}else {
			throw new bysjException(user.getUsername()+"有下属系，不能删除");
		}
		JdbcHelper.close(connection);
		return result;
	}

	public Actor login(User userToLogin, JSONObject message) throws SQLException {
		Connection connection = JdbcHelper.getConn();
		Actor actor = userDao.login(connection, userToLogin,message);
		JdbcHelper.close(connection);
		return actor;
	}

	public boolean selectUsername(String username) throws SQLException {
		Connection conn = JdbcHelper.getConn();
		Collection<User> all = userDao.findAll(conn);
		for (User user1 : all) {
			boolean equals = user1.getUsername().equals(username);
			return equals;
		}
		JdbcHelper.close(conn);
		return false;
	}
}
